package com.mt.roll.wms.dispatch.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.LocationStorageSub;
import com.mt.roll.core.dal.entity.StorageBoxCircInfo;
import com.mt.roll.core.dal.entity.StorageBoxCircInfoHis;
import com.mt.roll.core.dal.service.LocationStorageSubServiceBiz;
import com.mt.roll.core.dal.service.StorageBoxCircInfoHisServiceBiz;
import com.mt.roll.core.dal.service.StorageBoxCircInfoServiceBiz;
import com.mt.roll.wms.dispatch.DisPatchService;
import com.mt.roll.wms.params.StorageBoxCircInfoParam;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.List;


/**
 * @auther yulianchen
 * @date 2022/3/10 15:03
 */
public class DisPatchServiceImpl extends BaseService implements DisPatchService {

    @Resource
    private StorageBoxCircInfoServiceBiz storageBoxCircInfoServiceBiz;

    @Resource
    private LocationStorageSubServiceBiz locationStorageSubServiceBiz;

    @Resource
    private StorageBoxCircInfoHisServiceBiz storageBoxCircInfoHisServiceBiz;


    @Override
    public boolean locationEmpty(StorageBoxCircInfoParam storageBoxCircInfoParam) {
        //修改存储箱状态为空
        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getById(storageBoxCircInfoParam.getId());
        QueryWrapper<StorageBoxCircInfo> circInfoQueryWrapper = new QueryWrapper<>();
        circInfoQueryWrapper.eq(StorageBoxCircInfo.ID,storageBoxCircInfoParam.getId());
        Assert.geZero(storageBoxCircInfoServiceBiz.count(circInfoQueryWrapper), "未找到库位对应存储箱信息");
        StorageBoxCircInfo updateStorageBoxCircInfo = new StorageBoxCircInfo();
        BeanUtils.copyProperties(storageBoxCircInfoParam, updateStorageBoxCircInfo);
        updateStorageBoxCircInfo.setIsEmpty(0);
        //生成新的基板编码
        Long tempLocationStorageSubID = System.currentTimeMillis();
        updateStorageBoxCircInfo.setLocationStorageSubId(tempLocationStorageSubID);
        setUpdateCommonField(updateStorageBoxCircInfo);
        storageBoxCircInfoServiceBiz.updateById(updateStorageBoxCircInfo);
        //取到之前的基板编码，来取需要操作的基板
        Long locationStorageSubID = storageBoxCircInfo.getLocationStorageSubId();
        QueryWrapper<LocationStorageSub> subQueryWrapper = new QueryWrapper<>();
        subQueryWrapper.eq(LocationStorageSub.LOCATION_STORAGE_SUB_ID,locationStorageSubID);
        List<LocationStorageSub> subList = locationStorageSubServiceBiz.list(subQueryWrapper);
        for (LocationStorageSub sub : subList){
            sub.setStorageId(storageBoxCircInfo.getStorageBoxId());
            //插入的是新生成的编码
            sub.setLocationStorageSubId(tempLocationStorageSubID);
            //插入基板表
            locationStorageSubServiceBiz.save(sub);
        }
        //从缓存区取满箱
        Boolean result = getSubstrateFromBuffer(storageBoxCircInfoParam);
        return result;
    }

    @Override
    public boolean getSubstrateFromBuffer(StorageBoxCircInfoParam storageBoxCircInfoParam) {
        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getById(storageBoxCircInfoParam.getId());
        String localProcess = storageBoxCircInfo.getInProcessCode();
        //查表获取缓冲区的箱子，取第一个
        QueryWrapper<StorageBoxCircInfo> storageBoxCircInfoQueryWrapper = new QueryWrapper<>();
        storageBoxCircInfoQueryWrapper.eq(StorageBoxCircInfo.CURR_LOCATION_ID,localProcess)
                .eq(StorageBoxCircInfo.IS_EMPTY,1).eq(StorageBoxCircInfo.IS_PROCESS,1);
        StorageBoxCircInfo bufferCircInfo = storageBoxCircInfoServiceBiz.getOne(storageBoxCircInfoQueryWrapper);
        //TODO:调WCS接口
        Long processLocationID = storageBoxCircInfo.getCurrLocationId();
        Long bufferLocationID = bufferCircInfo.getCurrLocationId();

        Boolean result;
        result = true;

        if (result){
            Boolean exChangeResult = exChange(storageBoxCircInfo,bufferCircInfo);
        }
        return true;
    }

    @Override
    public boolean locationFull(StorageBoxCircInfoParam storageBoxCircInfoParam) {
        //修改存储箱状态为空
        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getById(storageBoxCircInfoParam.getId());
        QueryWrapper<StorageBoxCircInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StorageBoxCircInfo.ID,storageBoxCircInfoParam.getId());
        Assert.geZero(storageBoxCircInfoServiceBiz.count(queryWrapper), "未找到库位对应存储箱信息");
        StorageBoxCircInfo updateStorageBoxCircInfo = new StorageBoxCircInfo();
        BeanUtils.copyProperties(storageBoxCircInfoParam, updateStorageBoxCircInfo);
        updateStorageBoxCircInfo.setIsEmpty(1);
        //生成新的基板编码
        Long tempLocationStorageSubID = System.currentTimeMillis();
        updateStorageBoxCircInfo.setLocationStorageSubId(tempLocationStorageSubID);
        //TODO:去工单表找到下一个工序

        setUpdateCommonField(updateStorageBoxCircInfo);
        storageBoxCircInfoServiceBiz.updateById(updateStorageBoxCircInfo);
        //取到之前的基板编码，来取需要操作的基板
        Long locationStorageSubID = storageBoxCircInfo.getLocationStorageSubId();
        QueryWrapper<LocationStorageSub> subQueryWrapper = new QueryWrapper<>();
        subQueryWrapper.eq(LocationStorageSub.LOCATION_STORAGE_SUB_ID,locationStorageSubID);
        List<LocationStorageSub> subList = locationStorageSubServiceBiz.list(subQueryWrapper);
        for (LocationStorageSub sub : subList){
            sub.setStorageId(storageBoxCircInfo.getStorageBoxId());
            //插入的是新生成的编码
            sub.setLocationStorageSubId(tempLocationStorageSubID);
            //插入基板表
            locationStorageSubServiceBiz.save(sub);
        }
        //从缓冲区取空箱
        Boolean result = getEmptyBoxFromBuffer(storageBoxCircInfoParam);
        return result;
    }

    @Override
    public boolean getEmptyBoxFromBuffer(StorageBoxCircInfoParam storageBoxCircInfoParam) {
        //获取下一道工序和缓冲区位置
        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getById(storageBoxCircInfoParam.getId());
        String nextProcessCode = storageBoxCircInfo.getNextProcessCode();
        //查表获取缓冲区的箱子，取第一个
        QueryWrapper<StorageBoxCircInfo> storageBoxCircInfoQueryWrapper = new QueryWrapper<>();
        storageBoxCircInfoQueryWrapper.eq(StorageBoxCircInfo.IN_PROCESS_CODE,nextProcessCode)
                .eq(StorageBoxCircInfo.IS_EMPTY,0).eq(StorageBoxCircInfo.IS_PROCESS,1);
        StorageBoxCircInfo bufferCircInfo = storageBoxCircInfoServiceBiz.getOne(storageBoxCircInfoQueryWrapper);
        //TODO:调WCS接口
        Long processLocationID = storageBoxCircInfo.getCurrLocationId();
        Long bufferLocationID = bufferCircInfo.getCurrLocationId();

        //----------

        Boolean result;
        result = true;

        if (result){
            Boolean exChangeResult = exChange(storageBoxCircInfo,bufferCircInfo);
        }
        return true;
    }

    @Override
    public boolean sendSubToFirstProcess(StorageBoxCircInfoParam storageBoxCircInfoParam) {
        //入口进入第一个缓冲区

        StorageBoxCircInfo storageBoxCircInfo = storageBoxCircInfoServiceBiz.getById(storageBoxCircInfoParam.getId());
        String firstProcess = storageBoxCircInfo.getInProcessCode();
        //查表获取缓冲区的箱子，取第一个
        QueryWrapper<StorageBoxCircInfo> storageBoxCircInfoQueryWrapper = new QueryWrapper<>();
        storageBoxCircInfoQueryWrapper.eq(StorageBoxCircInfo.IN_PROCESS_CODE,firstProcess)
                .eq(StorageBoxCircInfo.IS_EMPTY,0).eq(StorageBoxCircInfo.IS_PROCESS,1);
        StorageBoxCircInfo bufferCircInfo = storageBoxCircInfoServiceBiz.getOne(storageBoxCircInfoQueryWrapper);
        //TODO:调WCS接口


        //----------
        Boolean result;
        result = true;

        //插入基板表，先根据唯一标识获取基板列表
        Long locationStorageSubID = storageBoxCircInfo.getLocationStorageSubId();
        QueryWrapper<LocationStorageSub> subQueryWrapper = new QueryWrapper<>();
        subQueryWrapper.eq(LocationStorageSub.LOCATION_STORAGE_SUB_ID,locationStorageSubID);
        List<LocationStorageSub> subList = locationStorageSubServiceBiz.list(subQueryWrapper);
        Long tempLocationStorageSubID = System.currentTimeMillis();
        for (LocationStorageSub sub : subList){
            sub.setStorageId(storageBoxCircInfo.getStorageBoxId());
            //插入的是新生成的编码
            sub.setLocationStorageSubId(tempLocationStorageSubID);
            locationStorageSubServiceBiz.save(sub);
        }
        return true;
    }

    /*执行两个存储箱的交换操作
        先获取两个库位信息
        存入历史表
        交换存储箱id，是否为空标志
     */
    private boolean exChange(StorageBoxCircInfo processStorageBoxCircInfo,StorageBoxCircInfo bufferStorageBoxCircInfo){
        //先存入历史表
        StorageBoxCircInfoHis processStorageBoxCircInfoHis = new StorageBoxCircInfoHis();
        StorageBoxCircInfoHis bufferStorageBoxCircInfoHis = new StorageBoxCircInfoHis();
        BeanUtils.copyProperties(processStorageBoxCircInfoHis,processStorageBoxCircInfo);
        BeanUtils.copyProperties(bufferStorageBoxCircInfoHis,bufferStorageBoxCircInfo);
        //两个都插入历史表
        storageBoxCircInfoHisServiceBiz.save(processStorageBoxCircInfoHis);
        storageBoxCircInfoHisServiceBiz.save(bufferStorageBoxCircInfoHis);
        //交换内容
        Long tempBoxID = processStorageBoxCircInfo.getStorageBoxId();
        String tempBoxCode = processStorageBoxCircInfo.getStorageCode();
        int tempEmpty = processStorageBoxCircInfo.getIsEmpty();
        String tempWorkOrderNo = processStorageBoxCircInfo.getWorkOrderNo();
        processStorageBoxCircInfo.setStorageBoxId(bufferStorageBoxCircInfo.getStorageBoxId());
        processStorageBoxCircInfo.setStorageCode(bufferStorageBoxCircInfo.getStorageCode());
        processStorageBoxCircInfo.setIsEmpty(bufferStorageBoxCircInfo.getIsEmpty());
        processStorageBoxCircInfo.setWorkOrderNo(bufferStorageBoxCircInfo.getWorkOrderNo());
        bufferStorageBoxCircInfo.setStorageBoxId(tempBoxID);
        bufferStorageBoxCircInfo.setStorageCode(tempBoxCode);
        bufferStorageBoxCircInfo.setIsEmpty(tempEmpty);
        bufferStorageBoxCircInfo.setWorkOrderNo(tempWorkOrderNo);
        bufferStorageBoxCircInfo.setInProcessCode(processStorageBoxCircInfo.getInProcessCode());
        bufferStorageBoxCircInfo.setNextProcessCode(processStorageBoxCircInfo.getNextProcessCode());
        bufferStorageBoxCircInfo.setLocationStorageSubId(processStorageBoxCircInfo.getLocationStorageSubId());
        //更新存储箱表
        storageBoxCircInfoServiceBiz.updateById(processStorageBoxCircInfo);
        storageBoxCircInfoServiceBiz.updateById(bufferStorageBoxCircInfo);
        return true;
    }
}
